package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@CrossOrigin
@RestController
public class ActivityIndexCheckController extends YxyApiBaseController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository yxyHuodongWeikeContestCatalogueRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/check", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(//
            HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        YxyProfile profile = getProfile(request, response);
        RespModel respModel = null;
        List<YxyHuodongWeikeContestCatalogue> list = yxyHuodongWeikeContestCatalogueRepository
                .findByUserIdOrderByCreatedDateAsc(profile.getId());
        List<RespModel> list2 = new ArrayList<RespModel>();
        for (YxyHuodongWeikeContestCatalogue yxy : list) {
            respModel = new RespModel();
            respModel.id = yxy.getId();
            respModel.phaseName = yxy.getPhaseName();
            respModel.subjectName = yxy.getSubjectName();
            respModel.versionName = yxy.getVersionName();
            respModel.semesterName = yxy.getSemesterName();
            respModel.catalogName = yxy.getBookContentName();
            respModel.knowledageName = yxy.getKnowNames();
            respModel.status = yxy.getStatus();
            if (yxy.getStatus()) {
                respModel.location = yxy_res_url + "/res/browse/" + yxy.getWeikeResId();
            }
            list2.add(respModel);
        }
        return list2;
    }

    protected static class ReqModel extends YxyApiReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String phaseName;
        public String subjectName;
        public String versionName;
        public String semesterName;
        public String catalogName;
        public String knowledageName;
        public Boolean status;
        public String location;
    }
}
